package activities;

import java.util.ArrayList;

import bookless.pakage.Book;
import bookless.pakage.BookHandler;
import bookless.pakage.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.LinearLayout;

public class BookSearchActivity extends Activity {

	private static Context context;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);

		context = BookSearchActivity.this;

		Button searchTitle;
		searchTitle = (Button) findViewById(R.id.searchTitle);
		searchTitle.setOnClickListener(new View.OnClickListener() {

			/**
        	 * 
        	 */
			@Override
			public void onClick(View v) {

				EditText editText = (EditText) findViewById(R.id.searchField);
				String title = editText.getText().toString();

				ArrayList<Book> booksList = new ArrayList<Book>();

				booksList = BookHandler.getBooksByTitle(title);

				printBooks(booksList);

			}
		});

		Button searchAuthor;
		searchAuthor = (Button) findViewById(R.id.searchAuthor);
		searchAuthor.setOnClickListener(new View.OnClickListener() {

			/**
        	 * 
        	 */
			@Override
			public void onClick(View v) {

				EditText editText = (EditText) findViewById(R.id.searchField);
				String author = editText.getText().toString();

				ArrayList<Book> booksList = new ArrayList<Book>();

				booksList = BookHandler.getBooksByAuthor(author);

				printBooks(booksList);

			}
		});

		Button submit;
		submit = (Button) findViewById(R.id.listbutton);
		submit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				ArrayList<Book> booksList = new ArrayList<Book>();

				booksList = BookHandler.getAllBooks();

				printBooks(booksList);
			}
		});
	}

	/**
	 * Iterated through a list with books, every book is put in a TextView, they
	 * are put on a layout that are shown in the phone.
	 * 
	 * @param booksList a list with books that you want to print in the screen.
	 */
	public void printBooks(ArrayList<Book> booksList) {

		int length = booksList.size();
		LinearLayout listLayout = new LinearLayout(this);
		setContentView(listLayout);
		listLayout.setOrientation(LinearLayout.VERTICAL);

		for (int i = 0; i < length; i++) {
			String text = booksList.get(i).toString();
			TextView tv = new TextView(getApplicationContext());
			tv.setText(text);
			listLayout.addView(tv);
		}

		Button back = new Button(getApplicationContext());
		back.setWidth(6);
		back.setText("Make a new search");
		listLayout.addView(back);

		back.setOnClickListener(new View.OnClickListener() {

			/**
        	 * 
        	 */
			@Override
			public void onClick(View v) {

				startActivity(new Intent("bookless.pakage.SEARCH"));
			}
		});
	}

	public static Context getContext() {
		return context;
	}
}
